package com.bijava.util;

/**
 * Created by ljj on 2018/2/5.
 */


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;


public class CodeGeneratorUtil {

    public static File BaseGen(Map data, String ftlName, String rootDir, String fileDir, String fileName) throws IOException, TemplateException {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setDirectoryForTemplateLoading(new File(rootDir));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        Template temp = cfg.getTemplate(ftlName);
        File dir = new File(fileDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, fileName);
        OutputStream fos = new FileOutputStream(file);
        Writer out = new OutputStreamWriter(fos);
        temp.process(data, out);
        fos.flush();
        fos.close();
        return file;
    }

}